<template>
  <a-form-model v-bind="{labelCol:{sm:7,xl:7},wrapperCol:{sm:14,xl:14}}"
                :model="form" :rules="rules" ref="formModel">
    <a-form-model-item label="创作初稿" prop="draft">
      <a-textarea v-model="form.draft" :rows="10"></a-textarea>
    </a-form-model-item>
    <a-form-model-item label="撰稿人" prop="interviewUnionId">
      <a-select
        mode="multiple"
        placeholder="请选择用户名称"
        optionFilterProp="children"
        allowClear
        labelInValue
        v-model="form.interviewUnionId">
        <a-select-option
          v-for="(item,index) in dictOptions"
          :key="index"
          :value="item.value">{{ item.text || item.label }}
        </a-select-option>
      </a-select>
    </a-form-model-item>
  </a-form-model>
</template>

<script>
  import {ajaxGetDictItems} from '@/api/api'
  import {getAction} from "../../api/manage";

  export default {
    name: "InterviewInitialDraft",
    props: ['params'],
    data() {
      return {
        dictOptions: [],
        form: {},
        rules: {
          draft: [{required: true, message: '请输入创作初稿', trigger: 'blur'}],
          interviewUnionId:[{required: true, message: '请选择撰稿人', trigger: 'blur'}],
        }
      }
    },
    created() {
      this.initDictData();
      //尝试
      this.queryDraftInfo();
    },
    methods: {
      queryDraftInfo() {
        getAction('task/interviewInfo/queryInterviewDraft', {taskNo: this.params.taskNo}).then(res => {
          if (res.success) {
            const tempForm = {
              draft: res.result.draft
            };
            if (res.result.interviewUnion) {
              tempForm.interviewUnionId = res.result.interviewUnion.map(item => {
                return {
                  key: item.userId,
                  label: item.userName
                }
              })
            }
            this.form = tempForm;
          }
        })
      },
      initDictData() {
        //根据字典Code, 初始化字典数组
        // ajaxGetDictItems('sys_user,realname,id', null).then((res) => {
        //   if (res.success) {
        //     this.dictOptions = res.result;
        //   }
        // });
        getAction("sys/user/pureList").then(res=>{
          if(res.success){
            this.dictOptions = res.result;
          }
        })
      },
      selfValidate() {
        return new Promise((resolve, reject) => {
          this.$refs.formModel.validate(validate => {
            if (validate) {
              if (this.form.interviewUnionId) {
                this.form.interviewUnion = this.form.interviewUnionId.map(item => {
                  return {
                    userId: item.key,
                    userName: item.label
                  }
                })
              }
              resolve({key: "interviewDraft", value: this.form})
            } else {
              reject('请填写采访初稿必要信息');
              return false
            }
          });
        });
      }
    }
  }
</script>

<style scoped>

</style>